package com.jackeysun.common

import android.app.Activity
import android.os.Process
import android.util.Log
import java.util.*
import kotlin.system.exitProcess

/**
 *   Created by sunxunchao on 2021/11/24.
 */
object ActivityManager {

    private val TAG = ActivityManager::class.simpleName

    /**
     * 维护Activity 的list
     */
    private val mActivates = Collections.synchronizedList(LinkedList<Activity>())


    /**
     * @param activity 作用说明 ：添加一个activity到管理里
     */
    fun Activity.pushActivity() {
        mActivates.add(this)
        Log.d(TAG, "activityList:size:" + mActivates.size)
    }

    /**
     * @param activity 作用说明 ：删除一个activity在管理里
     */
    fun Activity.popActivity() {
        mActivates.remove(this)
        Log.d(TAG, "activityList:size:" + mActivates.size)
    }


    /**
     * get current Activity 获取当前Activity（栈中最后一个压入的）
     */
    fun currentActivity(): Activity? =
        if (mActivates.isEmpty()) {
            null
        } else mActivates[mActivates.size - 1]

    /**
     * 结束当前Activity（栈中最后一个压入的）
     */
    fun finishCurrentActivity() {
        if (mActivates.isEmpty()) {
            return
        }
        val activity: Activity =
            mActivates[mActivates.size - 1]
        activity.finishActivity()
    }

    /**
     * 结束指定的Activity
     */
    fun Activity.finishActivity() {
        if (mActivates.isEmpty()) {
            return
        }
        mActivates.remove(this)
        if (!this.isFinishing) {
            this.finish()
        }
    }

    /**
     * 结束指定类名的Activity
     */
    fun Class<*>.finishActivity() {
        if (mActivates.isEmpty()) {
            return
        }
        for (activity in mActivates) {
            if (activity.javaClass == this) {
                activity.finishActivity()
            }
        }
    }

    /**
     * 按照指定类名找到activity
     *
     * @param cls
     * @return
     */
    fun Class<*>.findActivity(): Activity? {
        var targetActivity: Activity? = null
        for (activity in mActivates) {
            if (activity.javaClass == this) {
                targetActivity = activity
                break
            }
        }
        return targetActivity
    }

    /**
     * @return 作用说明 ：获取当前最顶部activity的实例
     */
    fun getTopActivity(): Activity? {
        var activity: Activity? = null
        synchronized(mActivates) {
            val size: Int = mActivates.size - 1
            if (size < 0) {
                return null
            }
            activity = mActivates[size]
        }
        return activity
    }

    /**
     * @return 作用说明 ：获取当前最顶部的acitivity 名字
     */
    fun getTopActivityName(): String? {
        var mBaseActivity: Activity? = null
        synchronized(mActivates) {
            val size: Int = mActivates.size - 1
            if (size < 0) {
                return null
            }
            mBaseActivity = mActivates[size]
        }
        return mBaseActivity?.javaClass?.name
    }

    /**
     * 退出应用程序
     */
    fun appExit() {
        try {
            Log.d(TAG, "app exit")
            finishAllActivity()
            exitProcess(0)
            Process.killProcess(Process.myPid())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun getActivates(): List<Activity> {
        return mActivates
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        for (activity in mActivates) {
            if (!activity.isFinishing) {
                activity.finish()
            }
        }
        mActivates.clear()
    }

    fun finishActivityExceptCurrent() {
        if (mActivates.size ==0) return
        val currentActivity = getTopActivity()
        mActivates.remove(currentActivity)
        for (activity in mActivates) {
            activity.finish()
        }
        mActivates.clear()
        mActivates.add(currentActivity)
    }

    fun Class<*>.finishActivityExcept() {
        if (mActivates.size == 0) return
        var i = 0
        while (i < mActivates.size) {
            if (mActivates[i].javaClass != this) {
                if (!mActivates[i].isFinishing) {
                    mActivates[i].finish()
                }
                mActivates.remove(mActivates[i])
                i--
            }
            i++
        }
    }
}